/**********************************************************************
*file:系统参数
*author:残梦
*versions:V1.0
*date:2025.1.21
*note:
**********************************************************************/
#include "system.h"
#include "cmsis_os.h"

typedef struct
{
    uint8_t ready:1;//系统就绪
    uint8_t :7;
}System_StateBits_StructDef;
typedef union
{
    uint8_t all;
    System_StateBits_StructDef bits;
}system_State_UnionDef;
typedef struct
{
    system_State_UnionDef state;
}System_Handle_StructDef;

System_Handle_StructDef *System_ParameterGet(void);

static int32_t System_BoardInit(void);

static System_Handle_StructDef SystemHandle = {\
    .state.all = 0
};

/***************************起点:驱动程序区域*******************************************/
/****************************************************
@function:获取系统参数指针
@param:void
@return:SystemHandle变量地址
@note:
****************************************************/
System_Handle_StructDef *System_ParameterGet(void)
{
    return &SystemHandle;
}

/****************************************************
@function:系统状态设置
@param:
    id--类型
    state--状态
@return:void
@note:
****************************************************/
void System_StateSet(System_StateID_EnumDef id,bool state)
{
    System_Handle_StructDef *sys = System_ParameterGet();

    switch(id)
    {
        case eSystem_StateID_Ready:sys->state.bits.ready = state;break;
    }
}

/****************************************************
@function:获取系统状态
@param:id--类型 
@return:状态
@note:
****************************************************/
bool System_StateGet(System_StateID_EnumDef id)
{
    System_Handle_StructDef *sys = System_ParameterGet();

    switch(id)
    {
        case eSystem_StateID_Ready:return (bool )sys->state.bits.ready;
        default:return false;
    }
}

/****************************************************
@function:freeRTOS系列线程等待系统准备就绪
@param:void
@return:void
@note:
****************************************************/
void System_ReadyWait(void)
{
    System_Handle_StructDef *sys = System_ParameterGet();
    while(!(sys->state.bits.ready))osDelay(1);
}

/****************************************************
@function:系统硬件初始化
@param:void
@return:初始化结果,小于0失败
    0--初始化成功
    -1--初始化失败
@note:
****************************************************/
int32_t System_Init(void)
{
    if(System_BoardInit() < 0)return -1;
    System_StateSet(eSystem_StateID_Ready,true);//系统就绪
    return 0;
}

/***************************终点:驱动程序区域*******************************************/
/***************************起点:应用程序区域*******************************************/
#include "common_driver.h"
#include "tim.h"
#include "gpio.h"
#include "can_driver.h"
#include "protocol_config_driver.h"
#include "adc.h"
#include "w25qxxx_driver.h"
#include "data.h"
#include "memory.h"
#define dSystemVersion "VprojectMCU_20250731_0"

/****************************************************
@function:板硬件初始化
@param:void
@return:初始化结果,小于0失败
    0--初始化成功
    -1--初始化失败
@note:
****************************************************/
static int32_t System_BoardInit(void)
{
    extern bool OperationInformationPrintf;

    OperationInformationPrintf = true;
    HAL_TIM_Base_Start_IT(&htim2);//FreeRTOS系统统计CPU使用率定时器
    W25QXXX_Init(eW25QXXX_DeviceID_W25Q32,System_osDelay);
    can_BoardInit();
    Data_Init();
    dLOG("address=%d Version:%s\r\n",System_BoardAddressGet(),dSystemVersion);   
    //Memory_FileDataPrintf(eMemory_FileObject_User0);
    //Memory_FileDataPrintf(eMemory_FileObject_VoltageTemperature);
    //Memory_FileDataPrintf(eMemory_FileObject_VoltageHydrogen);
    OperationInformationPrintf = false;
    VoltageSample_Start();
    return 0;
}

/****************************************************
@function:板硬件异常执行函数，此函数不予退出
@param:message:故障信息
@return:void
@note:
****************************************************/
void System_BoardFault(int8_t *message)
{
    //复位系统硬件

    while(1)
    {
        dLOG("Fault:%s",message);
        osDelay(1000);
    }
}

/****************************************************
@function:系统定时器1毫秒回调
@param:void
@return:void
@note:
****************************************************/
void System_BoardTimer1ms_Callback(void)
{
    static uint16_t ms = 0;
    ms++;
    ms = (ms > 1000)?0:ms;
}

void System_osDelay(uint32_t millisec){osDelay(millisec);}

/****************************************************
@function:板地址
@param:void
@return:板地址
@note:
****************************************************/
uint8_t System_BoardAddressGet(void)
{
    return Data_DeviceAddress_Get();
}
/***************************终点:应用程序区域*******************************************/
